    /*
 * CadastrarChegue.java
 *
 * Created on 02/09/2010, 14:27:48
 * Copyright Alex
 */
package Soligee.view;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.DecoradorInicializaCombos;
import Soligee.model.vo.lancamentos.BancoVO;
import Soligee.model.vo.lancamentos.ChequeVO;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.faces.data.DefaultSelectItemsArray;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class CadastrarChegue extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
    private SessionFacadeCadastrarImplements facadeCadastrar = new SessionFacadeCadastrarImplements();
    private static int aux=0;
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
   
   private DefaultSelectedData selectOneMenu1Bean = new DefaultSelectedData();

   public DefaultSelectedData getSelectOneMenu1Bean() {
      return selectOneMenu1Bean;
   }

   public void setSelectOneMenu1Bean(DefaultSelectedData dsd) {
      this.selectOneMenu1Bean = dsd;
   }
   private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

   public SelectInputDateBean getSelectInputDate1Bean() {
      return selectInputDate1Bean;
   }

   public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
      this.selectInputDate1Bean = sidb;
   }
   private SelectInputDateBean selectInputDateBean2 = new SelectInputDateBean();

   public SelectInputDateBean getSelectInputDateBean2() {
      return selectInputDateBean2;
   }

   public void setSelectInputDateBean2(SelectInputDateBean sidb) {
      this.selectInputDateBean2 = sidb;
   }
    private HtmlInputText campoCodCheque = new HtmlInputText();

    public HtmlInputText getCampoCodCheque() {
        return campoCodCheque;
    }

    public void setCampoCodCheque(HtmlInputText hit) {
        this.campoCodCheque = hit;
    }
    private HtmlInputText campoNumCheque = new HtmlInputText();

    public HtmlInputText getCampoNumCheque() {
        return campoNumCheque;
    }

    public void setCampoNumCheque(HtmlInputText hit) {
        this.campoNumCheque = hit;
    }
    private HtmlSelectOneMenu comboBanco = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboBanco() {
        return comboBanco;
    }

    public void setComboBanco(HtmlSelectOneMenu hsom) {
        this.comboBanco = hsom;
    }
    private HtmlInputText campoAgen = new HtmlInputText();

    public HtmlInputText getCampoAgen() {
        return campoAgen;
    }

    public void setCampoAgen(HtmlInputText hit) {
        this.campoAgen = hit;
    }

    private HtmlInputText campoEmitente = new HtmlInputText();

    public HtmlInputText getCampoEmitente() {
        return campoEmitente;
    }

    public void setCampoEmitente(HtmlInputText hit) {
        this.campoEmitente = hit;
    }
    private HtmlInputText campoObs = new HtmlInputText();

    public HtmlInputText getCampoObs() {
        return campoObs;
    }

    public void setCampoObs(HtmlInputText hit) {
        this.campoObs = hit;
    }
    private SelectInputDate dataLancamento = new SelectInputDate();

    public SelectInputDate getDataLancamento() {
        return dataLancamento;
    }

    public void setDataLancamento(SelectInputDate sid) {
        this.dataLancamento = sid;
    }
    private SelectInputDate dataDeposito = new SelectInputDate();

    public SelectInputDate getDataDeposito() {
        return dataDeposito;
    }

    public void setDataDeposito(SelectInputDate sid) {
        this.dataDeposito = sid;
    }
   private PopupBean panelPopup1Bean = new PopupBean();

   public PopupBean getPanelPopup1Bean() {
      return panelPopup1Bean;
   }

   public void setPanelPopup1Bean(PopupBean pb) {
      this.panelPopup1Bean = pb;
   }
    private PanelPopup panelPopUpBanco = new PanelPopup();

    public PanelPopup getPanelPopUpBanco() {
        return panelPopUpBanco;
    }

    public void setPanelPopUpBanco(PanelPopup pp) {
        this.panelPopUpBanco = pp;
    }
    private PanelPopup panelPopup = new PanelPopup();

    public PanelPopup getPanelPopup() {
        return panelPopup;
    }

    public void setPanelPopup(PanelPopup pp) {
        this.panelPopup = pp;
    }
    private HtmlInputText campNumConta = new HtmlInputText();

    public HtmlInputText getCampNumConta() {
        return campNumConta;
    }

    public void setCampNumConta(HtmlInputText hit) {
        this.campNumConta = hit;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private DefaultSelectedData defaultSelectedData1 = new DefaultSelectedData();

    public DefaultSelectedData getDefaultSelectedData1() {
        return defaultSelectedData1;
    }

    public void setDefaultSelectedData1(DefaultSelectedData dsd) {
        this.defaultSelectedData1 = dsd;
    }
    private DefaultSelectionItems selectOneMenu1DefaultItems1 = new DefaultSelectionItems();

    public DefaultSelectionItems getSelectOneMenu1DefaultItems1() {
        return selectOneMenu1DefaultItems1;
    }

    public void setSelectOneMenu1DefaultItems1(DefaultSelectionItems dsi) {
        this.selectOneMenu1DefaultItems1 = dsi;
    }
    private DefaultSelectItemsArray comboBancoDefaultItems = new DefaultSelectItemsArray();

    public DefaultSelectItemsArray getComboBancoDefaultItems() {
        return comboBancoDefaultItems;
    }

    public void setComboBancoDefaultItems(DefaultSelectItemsArray dsia) {
        this.comboBancoDefaultItems = dsia;
    }
    private DefaultSelectedData selectOneMenu2Bean = new DefaultSelectedData();

    public DefaultSelectedData getSelectOneMenu2Bean() {
        return selectOneMenu2Bean;
    }

    public void setSelectOneMenu2Bean(DefaultSelectedData dsd) {
        this.selectOneMenu2Bean = dsd;
    }
    private DefaultSelectionItems selectOneMenu2DefaultItems = new DefaultSelectionItems();

    public DefaultSelectionItems getSelectOneMenu2DefaultItems() {
        return selectOneMenu2DefaultItems;
    }

    public void setSelectOneMenu2DefaultItems(DefaultSelectionItems dsi) {
        this.selectOneMenu2DefaultItems = dsi;
    }
    private HtmlSelectOneMenu comboSituacao = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboSituacao() {
        return comboSituacao;
    }

    public void setComboSituacao(HtmlSelectOneMenu hsom) {
        this.comboSituacao = hsom;
    }
    private HtmlInputText campoValor = new HtmlInputText();

    public HtmlInputText getCampoValor() {
        return campoValor;
    }

    public void setCampoValor(HtmlInputText hit) {
        this.campoValor = hit;
    }
    private HtmlInputText campoCodBanco = new HtmlInputText();

    public HtmlInputText getCampoCodBanco() {
        return campoCodBanco;
    }

    public void setCampoCodBanco(HtmlInputText hit) {
        this.campoCodBanco = hit;
    }
    private HtmlInputText campoNomeBanco = new HtmlInputText();

    public HtmlInputText getCampoNomeBanco() {
        return campoNomeBanco;
    }

    public void setCampoNomeBanco(HtmlInputText hit) {
        this.campoNomeBanco = hit;
    }
    private SqlDateConverter sqlDateConverter1 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter1() {
        return sqlDateConverter1;
    }

    public void setSqlDateConverter1(SqlDateConverter sdc) {
        this.sqlDateConverter1 = sdc;
    }
    private HtmlCommandButton panelLayoutLogo = new HtmlCommandButton();

    public HtmlCommandButton getPanelLayoutLogo() {
        return panelLayoutLogo;
    }

    public void setPanelLayoutLogo(HtmlCommandButton hcb) {
        this.panelLayoutLogo = hcb;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public CadastrarChegue() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        inicializaCampos();
        
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("CadastrarChegue Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected SessionBean1 getview$SessionBean1() {
      return (SessionBean1) getBean("view$SessionBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected ApplicationBean1 getview$ApplicationBean1() {
      return (ApplicationBean1) getBean("view$ApplicationBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected RequestBean1 getview$RequestBean1() {
      return (RequestBean1) getBean("view$RequestBean1");
   }

   public void inicializaCampos(){
            limparCampos();
            panelPopup.setRendered(false);
            panelPopUpBanco.setRendered(false);
            campoCodCheque.setValue(facadeBusca.totalDeRegistros(ChequeVO.class)+1);
            selectOneMenu1DefaultItems1 = new DecoradorInicializaCombos().inicializarCampoBanco();
            selectOneMenu2DefaultItems.clear();
            selectOneMenu2DefaultItems.add(new SelectItem(1,"Devolvido"));
            selectOneMenu2DefaultItems.add(new SelectItem(2,"Pendente"));
            selectOneMenu2DefaultItems.add(new SelectItem(3,"Repassado"));
            selectOneMenu2DefaultItems.add(new SelectItem(4,"Compensado"));
        }
   public void limparCampos(){
        selectOneMenu1DefaultItems1 = new DecoradorInicializaCombos().inicializarCampoBanco();
        campoCodCheque.resetValue();
        campoNumCheque.resetValue();
        campoAgen.resetValue();
        campNumConta.resetValue();
        campoValor.resetValue();
        campoEmitente.resetValue();
        campoObs.resetValue();
        dataDeposito.resetValue();
        dataLancamento.resetValue();
    }
   public void controlaPopup(boolean renderedPopup, String textoPoup, boolean btn1Rendered, boolean btn2Rendered,
           String textobtn1, String textobtn2) {

      panelPopup.setRendered(renderedPopup);
      textoPopUp.setValue(textoPoup);
      btnPopUp1.setValue(textobtn1);
      btnPopUp1.setRendered(btn1Rendered);
      btnPopUp2.setRendered(btn2Rendered);
      btnPopUp2.setValue(textobtn2);
   }

    public String btnPopUp2_action() {
        controlaPopup(false, "", false, false, "", "");
        return null;
    }

    public String btnCadastrar_action() {
        if(comboBanco.getValue().toString().equals("Novo Banco") || Integer.parseInt(comboBanco.getValue().toString())==0){
            controlaPopup(true, "Selecione um Banco", false, true, "Sim", "Ok");
        }
        else{
        aux=1;
        controlaPopup(true, "Deseja Cadastrar Cheque?", true, true, "Sim", "Não");
        }
        return null;
    }

    public String btnPopUp1_action() {
        if(aux==0){
            limparCampos();
            return "voltar";
        }
        else if(aux==1){
            try {
                Date dataLancamento2 = (Date) dataLancamento.getValue();
                Date dataDeposito2 = (Date) dataDeposito.getValue();
 
            if (facadeCadastrar.cadastrarCheque(Integer.parseInt(campoCodCheque.getValue().toString()),campoAgen.getValue().toString(), campNumConta.getValue().toString(),
                        campoNumCheque.getValue().toString(), Integer.parseInt(comboSituacao.getValue().toString()),campoEmitente.getValue().toString(),
                        campoObs.getValue().toString(), dataLancamento2, dataDeposito2,
                        Double.parseDouble(campoValor.getValue().toString()) , Integer.parseInt(comboBanco.getValue().toString()))) {
                this.controlaPopup (true,"Cheque cadastrado com sucesso!",false,true,"","OK");
                limparCampos();
            }
        } catch (Exception e) {
            controlaPopup(true,"Erro:" + e.getMessage() + "caused by: " + e.getStackTrace(),false,true,"","OK");
            System.err.println("Erro:" + e.getMessage() + "caused by: " + e.getStackTrace());
            e.getStackTrace();
            }
        }else{
            try {
                if(facadeCadastrar.cadastrarBanco(Integer.parseInt(campoCodBanco.getValue().toString()), campoNomeBanco.getValue().toString(), true)){
                    selectOneMenu1DefaultItems1 = new DecoradorInicializaCombos().inicializarCampoBanco();
                    this.controlaPopup (true,"Banco cadastrado com sucesso!",false,true,"","OK");
                }
            } catch (Exception ex) {
                controlaPopup(true,"Erro:" + ex.getMessage() + "caused by: " + ex.getStackTrace(),false,true,"","OK");
                System.err.println("Erro:" + ex.getMessage() + "caused by: " + ex.getStackTrace());
                ex.getStackTrace();
            }
            
        }//fim do else
        return null;
    }

    public String btn_voltar_action() {
        aux=0;
        controlaPopup(true, "Deseja Sair?", true, true, "Sim", "Não");
        return null;
    }

    public void comboBanco_processValueChange(ValueChangeEvent vce) {
        if(comboBanco.getValue().equals("Novo Banco")){
            campoCodBanco.setValue(facadeBusca.totalDeRegistros(BancoVO.class)+1);
            panelPopUpBanco.setRendered(true);
        }
    }

    public String btDesconfirmaBanco_action() {
        campoNomeBanco.resetValue();
        panelPopUpBanco.setRendered(false);
        return null;
    }

    public String btConfirmaBanco_action() throws Exception {
       aux=2;
       panelPopUpBanco.setRendered(false);
       controlaPopup(true, "Confirma Cadastro de Banco", true, true, "Sim", "Não");
       return null;
    }

    public String panelLayoutLogo_action() {
        return "voltar";
    }

}

